package com.arvin.service.serviceImpl;

import com.arvin.mapper.ClassMapper;
import com.arvin.mapper.StudentMapper;
import com.arvin.mapper.TimeMapper;
import com.arvin.mapper.UserMapper;
import com.arvin.pojo.Time;
import com.arvin.service.StudentService;
import com.arvin.service.UserService;
import com.arvin.pojo.User;
import com.arvin.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.arvin.utils.Md5Util.getMD5String;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TimeMapper timeMapper;
    @Autowired
    private ClassMapper classMapper;
    //根据用户名查找
    @Override
    public User findByUsername(String studentId) {
        User u = userMapper.findByUsername(studentId);
        return u;
    }
    //注册
    @Override
    @Transactional
    public void register(String studentId, String password,String email) {
        //密码MD5加密
        String md5String = getMD5String(password);
        Time time = timeMapper.getTime();
        userMapper.add(studentId,md5String,email,time.getClassName());
        classMapper.add(studentId);
    }
    //修改密码
    @Override
    public void updatePwd(String newpwd,String studentId) {
        userMapper.updatePwd(Md5Util.getMD5String(newpwd),studentId);
    }
}
